const process = require('process')
    //1. 连接数据库
require('./connect/connect.js');
//2. 引入骨架
const { userSchema, carSchema, position_addSchema } = require('./schema/index.js');
//3. 引入模型
const { usersModel, carModel, position_addModel } = require('./model/index.js')



const Mongo = {
    //增删改查
    position: {
        add(data) {
            //4. 模型实例化得到实体
            let position_add = new position_addModel(data);
            position_addModel.find((err, res) => {
                if (!err) {
                    if (res) {
                        let f = true; //定义一个开关
                        res.forEach((val, index) => {
                            if (val.companyName === data.companyName && val.positionName === data.positionName) {
                                f = false;
                                return
                            }
                        });
                        if (f) {
                            position_add.save()
                        }
                    }
                }
            })
        },
        find() {
            //数据库的查询
            let p1 = position_addModel.find({}).then(res => res)
            return p1
        },
        remove(id) {
            const p1 = position_addModel.findById(id, function(err, res) {
                if (!err) {
                    console.log('删除成功')
                    if (res) {
                        res.remove()
                    }
                    return
                }
            }).then(res => {
                if (res) {
                    return true
                } else {
                    return false
                }
            }).catch(err => false)
            return p1
        }
    }
}


module.exports = {
    user: Mongo.user,
    car: Mongo.car,
    position: Mongo.position
}